#include <bits/stdc++.h>

using namespace std;

int n;
vector<int> father(1001, 0);

void init()
{
    for (int i = 1; i <= n; i++)
    {
        father[i] = i;
    }
}

int find(int u)
{
    return father[u] == u ? u : father[u] = find(father[u]);
}

bool same(int u, int v)
{
    u = find(u);
    v = find(v);
    if (u == v)
        return true;
    return false;
}

void join(int u, int v)
{
    u = find(u);
    v = find(v);
    if (u == v)
        return;
    father[v] = u;
}

bool is_direction_tree(vector<vector<int>> &graph, int index)
{
    init();
    for (int i = 0; i < n; i++)
    {
        if (i == index)
        {
            continue;
        }
        if (same(graph[i][0], graph[i][1]))
        {
            return false;
        }
        join(graph[i][0], graph[i][1]);
    }
    return true;
}

void delete_loop(vector<vector<int>> &graph)
{
    init();
    for (int i = 0; i < n; i++)
    {
        if (same(graph[i][0], graph[i][1]))
        {
            cout << graph[i][0] << " " << graph[i][1] << endl;
        }
        join(graph[i][0], graph[i][1]);
    }
}

int main()
{
    int s, t;
    cin >> n;
    vector<vector<int>> graph;
    vector<int> degree(n + 1, 0);
    for (int i = 0; i < n; i++)
    {
        cin >> s >> t;
        degree[t]++;
        graph.push_back({s, t});
    }
    vector<int> vec;
    for (int i = n - 1; i >= 0; i--)
    {
        if (degree[graph[i][1]] == 2)
        {
            vec.push_back(i);
        }
    }
    if (!vec.empty())
    {
        if (is_direction_tree(graph, vec[0]))
        {
            cout << graph[vec[0]][0] << " " << graph[vec[0]][1] << endl;
            return 0;
        }
        else
        {
            cout << graph[vec[1]][0] << " " << graph[vec[1]][1] << endl;
            return 0;
        }
    }
    delete_loop(graph);
    return 0;
}